package com.pts.servlet.enterprise;

import com.pts.dao.Dao;
import com.pts.dao.EnterpriseDao;
import com.pts.vo.Enterprise;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//处理enterprise/list页面请求，转发到Yes/No页面
//功能是通过id更新公司信息

@WebServlet(value = "/entupdate")
public class UpdateServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String username = request.getParameter("username");
        request.setAttribute("username",username);
        request.setAttribute("result","修改");
        String str = request.getParameter("id");
        String name = request.getParameter("name");
        String place = request.getParameter("place");
        String tel = request.getParameter("tel");
        if("".equals(str)||str==null||"".equals(name)||"".equals(place)||"".equals(tel)||name==null||place==null||tel==null){
            request.getRequestDispatcher("admin/enterprise/Error.jsp").forward(request,response);
            return;
        }
        int id = Integer.parseInt(str);
        Enterprise enterprise = new Enterprise();
        enterprise.setId(id);
        enterprise.setName(name);
        enterprise.setPlace(place);
        enterprise.setTel(tel);
        Dao enterpriseDao = new EnterpriseDao();
        if(enterpriseDao.update(enterprise)){
            request.getRequestDispatcher("admin/enterprise/Yes.jsp").forward(request,response);
        }
        else{
            request.getRequestDispatcher("admin/enterprise/Error.jsp").forward(request,response);
        }
    }
}